import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class demo1 {
    class Solution {
        public int numberOfArithmeticSlices(int[] nums) {
            int n = nums.length;
            HashMap<Long, List<Integer>> map = new HashMap<>();
            for (int i = 0; i < n; i++) {
                if (!map.containsKey((long) nums[i])) {
                    map.put((long) nums[i], new ArrayList<>());
                }
                map.get((long) nums[i]).add(i);
            }
            int sum = 0;
            int[][] dp = new int[n][n];//表示以i j 后那个位置的时等差数组的数量
            for (int i = 2; i < n; i++) {
                for (int j = 1; j < i; j++) {
                    long ret = (long) (2L * nums[j] - nums[i]);//注意要2L防止乘2后溢出
                    if (map.containsKey(ret)) {
                        for (int x : map.get(ret)) {
                            if (x < j) {
                                dp[j][i] += dp[x][j] + 1;
                            } else break;
                        }
                    }
                    sum += dp[j][i];
                }
            }
            return sum;
        }
    }
}
